package com.yxjx.duoxue.customview;

import java.util.Calendar;
import java.util.List;

import com.yxjx.duoxue.R;
import com.yxjx.duoxue.course.CourseListActivity;
import com.yxjx.duoxue.course.datamodel.AdDTS;
import com.yxjx.duoxue.course.datamodel.AdListDTS;
import com.yxjx.duoxue.course.datamodel.HomeAllAdData;
import com.yxjx.duoxue.datamodel.DMObject;
import com.yxjx.duoxue.datamodel.RushDTS;
import com.yxjx.duoxue.global.Globals;
import com.yxjx.duoxue.push.PushHelper;
import com.yxjx.duoxue.utils.UIHelper;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * 除了banner之外的其他广告区域：抢购，四格广州，横条广告
 * @author mengmengchen
 *
 */
public class HomeAllAdsView extends LinearLayout {
	

	private View section1;
	private View section2;
	private View section3;
	private int[] section1Items;
	private View section1More;
	private int[] section2Items;
	private int[] section3Items;
	private RushDTS rush;
	private List<AdDTS> area4;
	private List<AdDTS> area5;
	private Context mContext;

	public HomeAllAdsView(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater.from(context).inflate(R.layout.segment_all_ads_view,
				this, true);
		
		mContext = context;
		
		section1 = findViewById(R.id.section1);
		section2 = findViewById(R.id.section2);
		section3 = findViewById(R.id.section3);
		
		section1Items = new int[] {
				R.id.section1_item1,
				R.id.section1_item2,
				R.id.section1_item3
		};
		section1More = section1.findViewById(R.id.section1_more);
		
		section2Items = new int[] {
				R.id.section2_item1,
				R.id.section2_item2,
				R.id.section2_item3,
				R.id.section2_item4
		};
		
		section3Items = new int[] {
				R.id.section3_item1
		};
	}

	/**
	 * update UI by HomeAllAdsData in Globals
	 */
	public void updateUI() {
		HomeAllAdData allAdsData = Globals.getInstance(getContext()).getAllAdsData();
		if (allAdsData == null) {
			setVisibility(View.GONE);
			return;
		}
		
		rush = allAdsData.getRush();
		area4 = allAdsData.getArea4();
		area5 = allAdsData.getArea5();
		
		// rush区域1
		if (rush == null || rush.getList() == null || rush.getList().size() == 0) {
			section1.setVisibility(View.GONE);
		} else {
			updateRushUIInternal();
		}
		
		// 四格广告
		if (area4 == null || area4.size() == 0) {
			section2.setVisibility(View.GONE);
		} else {
			updateArea4UIInternal();
		}
		
		// 单条广告
		if (area5 == null || area5.size() == 0) {
			section3.setVisibility(View.GONE);
		} else {
			updateArea5UIInternal();
		}
	}

	private void updateArea5UIInternal() {
		for (int index = 0; index < section3Items.length && index < area5.size(); index++) {
			AdDTS data = area5.get(index);
			if (data == null) {
				continue;
			}
			UIHelper.asyncLoadImage((ImageView) section3.findViewById(section3Items[index]), data.getAdImage(), R.drawable.default_subject);
			UIHelper.setClickListener(section3, section3Items[index], new ClickListener(data));
		}
	}

	private void updateArea4UIInternal() {
		for (int index = 0; index < section2Items.length && index < area4.size(); index++) {
			AdDTS data = area4.get(index);
			if (data == null) {
				continue;
			}
			UIHelper.asyncLoadImage((ImageView) section2.findViewById(section2Items[index]), data.getAdImage(), R.drawable.default_subject);
			UIHelper.setClickListener(section2, section2Items[index], new ClickListener(data));
		}
	}

	private void updateRushUIInternal() {
		// 倒计时
		DownTimeCounter timerView = (DownTimeCounter) section1.findViewById(R.id.down_timer);
		timerView.setStartEndTime(rush.getStartTime(), rush.getEndTime());
		// 抢购标题
		UIHelper.setText(section1, R.id.hot_title, rush.getHot());
		// 三项
		for (int index = 0; index < section1Items.length && index < rush.getList().size(); index++) {
			AdListDTS data = rush.getList().get(index);
			if (data == null) {
				continue;
			}
			UIHelper.asyncLoadImage((ImageView) section1.findViewById(section1Items[index]), data.getAdImage(), R.drawable.default_subject);
			UIHelper.setClickListener(section1, section1Items[index], new ClickListener(data));
		}
		// 点击更多
		AdDTS moreAd = new AdDTS();
		moreAd.setAdDes(rush.getDes());
		moreAd.setAdTitle(rush.getTitle());
		moreAd.setAdUrl(rush.getUrl());
		moreAd.setIsShare(0);
		section1More.setOnClickListener(new ClickListener(moreAd));
	}
	
	class ClickListener implements View.OnClickListener {

		private DMObject data;
		public ClickListener(DMObject data) {
			this.data = data;
		}
		
		@Override
		public void onClick(View arg0) {
			if (data == null) {
				return;
			}
			if (data instanceof AdDTS) {
				PushHelper.pushActivity(mContext, PushHelper.AdDTS2Custom((AdDTS) data));
			} else if (data instanceof AdListDTS) {
				PushHelper.pushActivity(mContext, PushHelper.AdDTS2Custom(AdDTS.fromAdListDTS((AdListDTS) data)));
			}
		}
	}
	
}
